using BasicScript.Component;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class ClothingMachineItem : BaseItem
	{
		public enum Anim
		{
			Static = 0,
			MoveUp1 = 1,
			StayDown = 2,
			MoveDown1 = 3,
			MoveDown2 = 4
		}

		private const float UP_POSITION = 1.84f;

		private const float DOWN_POSITION = 0.58f;

		private int iconId;

		private int minIconId;

		private int maxIconId;

		private int selectClothingIndex;

		private Transform clothingSelector;

		private Transform door;

		private Transform leftBtn;

		private Transform rightBtn;

		private Anim anim;

		private float animTimer;

		private AssetBundleManager abManager;

		private SpriteRenderer displaySpriteRenderer;

		private FrameAnimation leftLightAnim;

		private FrameAnimation rightLightAnim;

		private FrameAnimation tipsAnim;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			Transform transform = model.Find("Front/Display");
			transform.SetLocalZ(-2E-05f);
			leftBtn = transform.Find("LeftBtn");
			leftBtn.gameObject.layer = 15;
			rightBtn = transform.Find("RightBtn");
			rightBtn.gameObject.layer = 15;
			displaySpriteRenderer = transform.GetComponent<SpriteRenderer>();
			clothingSelector = model.Find("Front/ClothingSelector");
			clothingSelector.SetLocalZ(-2E-05f);
			int childCount = clothingSelector.childCount;
			for (int i = 0; i < childCount; i++)
			{
				clothingSelector.GetChild(i).gameObject.layer = 15;
			}
			abManager = Singleton<AssetBundleManager>.Instance;
			minIconId = 1;
			if (abManager.topsIcons != null)
			{
				maxIconId = abManager.topsIcons.Count;
			}
			door = frontMask.Find("Door");
			Transform transform2 = frontMask.Find("Mask");
			transform2.SetLocalZ(-1E-05f);
			Transform transform3 = transform2.Find("LightLeft");
			transform3.SetLocalZ(-1E-05f);
			leftLightAnim = transform3.GetComponent<FrameAnimation>();
			Transform transform4 = transform2.Find("LightRight");
			transform4.SetLocalZ(-1E-05f);
			rightLightAnim = transform4.GetComponent<FrameAnimation>();
			Transform transform5 = transform2.Find("Tips");
			transform5.SetLocalZ(-1E-05f);
			tipsAnim = transform5.GetComponent<FrameAnimation>();
			Transform trans = transform2.Find("TipsMask");
			trans.SetLocalZ(-2E-05f);
			iconId = data.state;
			SetIcon(iconId);
			if (childItems.Count < 1)
			{
				door.SetLocalY(0.58f);
			}
		}

		protected override void LateUpdate()
		{
			if (anim == Anim.Static)
			{
				return;
			}
			if (anim == Anim.MoveDown1 || anim == Anim.MoveDown2)
			{
				animTimer += Time.deltaTime;
				door.SetLocalY(Mathf.Lerp(1.84f, 0.58f, animTimer));
				if (animTimer >= 1f)
				{
					animTimer = 0f;
					if (anim == Anim.MoveDown1)
					{
						anim = Anim.StayDown;
						leftLightAnim.Play(true);
						rightLightAnim.Play(true);
						tipsAnim.Play(true);
					}
					else
					{
						anim = Anim.Static;
					}
				}
			}
			else if (anim == Anim.StayDown)
			{
				animTimer += Time.deltaTime;
				if (!(animTimer >= 2f))
				{
					return;
				}
				for (int i = 0; i < childItems.Count; i++)
				{
					if (childItems[i].itemData.scriptType == 14)
					{
						GlobalEventSystem.Fire(new BaseEvent("game.remove_item", childItems[i].archiveData.index));
					}
				}
				Transform child = clothingSelector.GetChild(selectClothingIndex);
				BaseItem baseItem = Singleton<Scene>.Instance.CreateItem(child.name, base.transform.position, 0);
				if (baseItem != null)
				{
					AttachItem(baseItem, DepthType.Front, AttachType.PlacePoint, true);
					((ClothingItem)baseItem).CheckState();
					((ClothingItem)baseItem).SetIcon(iconId);
					AddCreateItem(baseItem);
					baseItem.SetColliderEnabled(false);
					baseItem.depthSystem.SortBaseNode();
					Singleton<AudioManager>.Instance.PlaySound("sound_explode");
					Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", base.transform.position + new Vector3(0f, -5.39f, -0.002f), 1f);
					Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", base.transform.position + new Vector3(0f, -5.39f, -0.002f), 3f);
				}
				animTimer = 0f;
				anim = Anim.MoveUp1;
			}
			else
			{
				if (anim != Anim.MoveUp1)
				{
					return;
				}
				animTimer += Time.deltaTime;
				door.SetLocalY(Mathf.Lerp(0.58f, 1.84f, animTimer));
				if (animTimer >= 1f)
				{
					animTimer = 0f;
					anim = Anim.Static;
					for (int j = 0; j < childItems.Count; j++)
					{
						if (childItems[j].itemData.scriptType == 14)
						{
							childItems[j].SetColliderEnabled(true);
						}
					}
				}
				leftLightAnim.Stop();
				leftLightAnim.GotoFirstFrame();
				rightLightAnim.Stop();
				rightLightAnim.GotoFirstFrame();
				tipsAnim.Stop();
				tipsAnim.GotoFirstFrame();
			}
		}

		private void SetIcon(int id)
		{
			if (iconId < minIconId)
			{
				iconId = maxIconId;
			}
			else if (iconId > maxIconId)
			{
				iconId = minIconId;
			}
			else
			{
				iconId = id;
			}
			displaySpriteRenderer.sprite = abManager.GetTopsIcon(iconId);
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			if (anim != 0)
			{
				return;
			}
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (!(raycastHit2D.collider != null) || !(raycastHit2D.collider.transform.parent.parent.parent == model))
			{
				return;
			}
			if (raycastHit2D.collider.transform.name.Equals("LeftBtn"))
			{
				SetIcon(--iconId);
				SetBtnState(leftBtn, true);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
			else if (raycastHit2D.collider.transform.name.Equals("RightBtn"))
			{
				SetIcon(++iconId);
				SetBtnState(rightBtn, true);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
			else
			{
				if (!(raycastHit2D.collider.transform.parent == clothingSelector))
				{
					return;
				}
				if (archiveData.createIndexs.Count >= base.itemData.createAmount)
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_error");
					return;
				}
				int childCount = clothingSelector.childCount;
				for (int i = 0; i < childCount; i++)
				{
					if (!(clothingSelector.GetChild(i) == raycastHit2D.collider.transform))
					{
						continue;
					}
					selectClothingIndex = i;
					SetBtnState(raycastHit2D.collider.transform, true);
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
					for (int j = 0; j < childItems.Count; j++)
					{
						if (childItems[j].itemData.scriptType == 14)
						{
							childItems[j].SetColliderEnabled(false);
						}
					}
					if (childItems.Count > 0)
					{
						anim = Anim.MoveDown1;
					}
					else
					{
						anim = Anim.StayDown;
						leftLightAnim.Play(true);
						rightLightAnim.Play(true);
						tipsAnim.Play(true);
					}
					animTimer = 0f;
					break;
				}
			}
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			base.ReleaseAttachItem(item);
			if (childItems.Count < 1)
			{
				anim = Anim.MoveDown2;
				animTimer = 0f;
			}
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			int childCount = clothingSelector.childCount;
			for (int i = 0; i < childCount; i++)
			{
				SetBtnState(clothingSelector.GetChild(i), false);
			}
			SetBtnState(leftBtn, false);
			SetBtnState(rightBtn, false);
		}

		private void SetBtnState(Transform trans, bool isDown)
		{
			trans.Find("Down").SetObjectActive(isDown);
			trans.Find("Up").SetObjectActive(!isDown);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = iconId;
		}
	}
}
